/**
 * Copyright (c) 2002-2011 旭鸣信息
 * All rights reserved. 
 * File：GoodsGroupService.java
 * History:
 *       Apr 16, 2012: Initially created, liuzhi.
 */
package com.xteam.ecsystem.service.admin;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.xteam.ecsystem.dao.IGoodsGroupDao;

/**
 * 商品分类业务管理层
 * @author liuzhi
 *
 */
@Service
@SuppressWarnings("unchecked")
public class GoodsGroupService {
	
	@Autowired
	private IGoodsGroupDao goodsGroupDao;
	
	/**
	 * 
	 * 新增或者修改商品分类信息
	 * @author liuzhi
	 * @param obj
	 * @param id
	 */
	public String saveOrUpdateObject(Object obj, String id){
		return this.goodsGroupDao.saveOrUpdateObject(obj, id);
	}
	
	/**
	 * 
	 * 条件查询和分页显示商品分类信息（预留）
	 * @author liuzhi
	 * @param index
	 * @param size
	 * @param bggName
	 * @return
	 */
	public String getGoodsGroupList(int pageIndex, int pageSize, String bggName){
		return this.goodsGroupDao.getGoodsGroupList(pageIndex, pageSize, bggName);
	}
	
	/**
	 * 
	 * 级联删除商品分类信息
	 * @author liuzhi
	 * @param ids
	 */
	public void delObject(String id){
		this.goodsGroupDao.delObject(id);
	}

	/**
	 * 根据商品分类编号获得对应的商品分类信息
	 * @author liuzhi
	 * @param id
	 * @return
	 */
	public String getGoodsGroupInfoById(String id){
		return this.goodsGroupDao.getGoodsGroupInfoById(id);
	}
	
	/**
	 * 
	 * 根据商品分类编号修改商品分类状态
	 * 0：无效
	 * 1：有效
	 * @author liuzhi
	 * @param id
	 */
	public void updateBggStateById(String id){
		this.goodsGroupDao.updateBggStateById(id);
	}
	
	/**
	 * 
	 * 修改是否精品推荐
	 * 0：非精品
	 * 1：精品
	 * @author liuzhi
	 * @param id
	 */
	public void updateBggBestById(String id){
		this.goodsGroupDao.updateBggBestById(id);
	}
	
	/**
	 * 
	 * 修改是否新品推荐
	 * 0：非新品
	 * 1：新品
	 * @author liuzhi
	 * @param id
	 */
	public void updateBggNewById(String id){
		this.goodsGroupDao.updateBggNewById(id);
	}
	
	/**
	 * 
	 * 修改是否热销推荐
	 * 0：非热销
	 * 1：热销
	 * @author liuzhi
	 * @param id
	 */
	public void updateBggHotById(String id){
		this.goodsGroupDao.updateBggHotById(id);
	}
	
	/**
	 * 
	 * 修改是否团购推荐
	 * 0：非团购推荐
	 * 1：团购推荐
	 * @author liuzhi
	 * @param id
	 */
	public void updateBggBuyById(String id){
		this.goodsGroupDao.updateBggBuyById(id);
	}
	
	/**
	 * 获得商品分类TreeGrid(分页treegrid)
	 * @author liuzhi
	 * @param request
	 * @param pageIndex
	 * @param pageSize
	 * @param bggName
	 * @return
	 */
	public String getGoodsGroupTreeGrid_backup(HttpServletRequest request,
			int pageIndex, int pageSize, String bggName){
		/*{
			"total":3,
			"data":[
			        {"_ViewIndex":0,"_IsLeaf":false,"bggParent":"-1","_Expanded":true,"bggName":"3G手
			机","_Level":0,"bggId":"1000000001"}," 
					{"_ViewIndex":1,"bggParent":"1000000001","_Expanded":true,"bggName":"双模手
			机","_Level":1,"bggId":"1000000002"}," 
					{"_ViewIndex":2,"bggParent":"1000000001","_Expanded":true,"bggName":"CDMA手
			机","_Level":1,"bggId":"1000000003"}" 
				   ]
		}*/
		
		JSONArray jsonArray = this.goodsGroupDao.getGoodsGroupJsonArray(null, null);
		
		PluSoft.Data.DataTree tree = new PluSoft.Data.DataTree();
	    tree.IdField = "bggId";
	    tree.NodesField = "children";
	    tree.ParentField = "bggParent";
		tree.LoadExpandCollapseConfig(request);
		
		ArrayList list = (ArrayList)PluSoft.Utils.JSON.Decode(jsonArray.toString());
	    tree.LoadTree(list);
	    
	    //按分类名称模糊查询
	    if (bggName != null && bggName.length() > 0
				&& !bggName.equals("undefined")
				&& !bggName.equals("null")){
	        ArrayList filters = findNodesByCondition(bggName, tree.GetList());
	        tree.CreateDataView(filters);
	    }
	    
	    //用于存储总记录数和数据列表对象
	    Hashtable table = new Hashtable();
	    
	    //获取折叠、过滤、排序后的总记录数
	    int total = tree.GetCollapsedCount(); 
	    table.put("total", total);
	    
	    //从数据视图中获取分页数据
	    ArrayList data = tree.GetPagedNodes(pageIndex, pageSize);     
	    table.put("data", data);
	
	    String json = PluSoft.Utils.JSON.Encode(table);
	    return json;
	}
	
	/**
	 * 获得商品分类TreeGrid
	 * @author liuzhi
	 * @param request
	 * @param bggName
	 * @return
	 */
	public String getGoodsGroupTreeGrid(String bggName){		
		JSONArray jsonArray = this.goodsGroupDao.getGoodsGroupJsonArray(null, null);
		return jsonArray.toString();
	}
	
	/**
	 * 模糊查找节点信息
	 * @author liuzhi
	 * @param bggName
	 * @param nodeList
	 * @return
	 */
	private ArrayList findNodesByCondition(String bggName, ArrayList nodeList){
	    ArrayList filters = new ArrayList();

	    for (int i = 0, l = nodeList.size(); i < l; i++){
	        Map node = (Map)nodeList.get(i);
	        String condition = node.get("bggName").toString();
	        
	        if (condition != null && condition.indexOf(bggName) != -1){
	            filters.add(node);
	        }
	    }

	    return filters;
	}
	
	/**
	 * 懒加载商品分类树
	 * @author liuzhi
	 * @param bggParent
	 * @return
	 */
	public String lazyLoadGoodsGroupTree(String bggParent){
		return this.goodsGroupDao.lazyLoadGoodsGroupTree(bggParent);
	}
	
	/**
	 * 获得商品类型列表，以设置商品类型（属性配置）下拉组合框
	 * @author liuzhi
	 * @return
	 */
	public String getGoodsTypeList(){
		return this.goodsGroupDao.getGoodsTypeList();
	}
	
	/**
	 * 获得商品类型属性列表，以设置类型类型（属性配置）树形选择
	 * @author liuzhi
	 * @param bgtId
	 * @return
	 */
	public String getGoodsTypeAttrList(String bgtId){
		return this.goodsGroupDao.getGoodsTypeAttrList(bgtId);
	}
	
	/**
	 * 懒加载商品分类树形表格
	 * @author liuzhi
	 * @param bggParent
	 * @return
	 */
	public String lazyLoadGoodsGroupTreeGrid(String bggParent){
		return this.goodsGroupDao.lazyLoadGoodsGroupTreeGrid(bggParent);
	}

	/**
	 * 检验类型属性是否被占用
	 * @author liuzhi Oct 17, 2013
	 * @param bgaTattris
	 * @return
	 */
	public boolean getBgaByBgaTattris(String bgaTattris) {
		return this.goodsGroupDao.getBgaByBgaTattris(bgaTattris);
	}

	/**
	 * 检查该分类及其子分类下面是否有相应的商品
	 * @author liuzhi Oct 18, 2013
	 * @param bggId
	 * @return
	 */
	public boolean getBgiByBgg(String bggId) {
		return this.goodsGroupDao.getBgiByBgg(bggId);
	}
}
